package com.wande.dataplatform.controller;

import com.wande.common.core.domain.R;
import com.wande.common.log.annotation.Log;
import com.wande.common.log.enums.BusinessType;
import com.wande.common.mybatis.core.page.PageQuery;
import com.wande.common.mybatis.core.page.TableDataInfo;
import com.wande.common.web.core.BaseController;
import com.wande.dataplatform.domain.dto.EtlTaskDTO;
import com.wande.dataplatform.domain.vo.EtlInstanceVO;
import com.wande.dataplatform.domain.vo.EtlTaskVO;
import com.wande.dataplatform.etl.executor.EtlProgress;
import com.wande.dataplatform.service.IEtlTaskService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * ETL任务管理控制器
 *
 * @author dataplatform
 */
@Validated
@RestController
@RequestMapping("/dataplatform/etl/tasks")
@RequiredArgsConstructor
@Tag(name = "ETL任务管理", description = "ETL任务的增删改查和执行管理")
public class EtlTaskController extends BaseController {

    private final IEtlTaskService etlTaskService;

    /**
     * 创建ETL任务
     */
    @PostMapping
    @Operation(summary = "创建ETL任务")
    @Log(title = "ETL任务管理", businessType = BusinessType.INSERT)
    public R<Long> createTask(@Valid @RequestBody EtlTaskDTO dto) {
        Long taskId = etlTaskService.createTask(dto);
        return R.ok(taskId);
    }

    /**
     * 更新ETL任务
     */
    @PutMapping("/{id}")
    @Operation(summary = "更新ETL任务")
    @Log(title = "ETL任务管理", businessType = BusinessType.UPDATE)
    public R<Void> updateTask(
        @Parameter(description = "任务ID") @PathVariable Long id,
        @Valid @RequestBody EtlTaskDTO dto
    ) {
        dto.setId(id);
        etlTaskService.updateTask(dto);
        return R.ok();
    }

    /**
     * 删除ETL任务
     */
    @DeleteMapping("/{id}")
    @Operation(summary = "删除ETL任务")
    @Log(title = "ETL任务管理", businessType = BusinessType.DELETE)
    public R<Void> deleteTask(@Parameter(description = "任务ID") @PathVariable Long id) {
        etlTaskService.deleteTask(id);
        return R.ok();
    }

    /**
     * 获取ETL任务详情
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取ETL任务详情")
    public R<EtlTaskVO> getTask(@Parameter(description = "任务ID") @PathVariable Long id) {
        EtlTaskVO task = etlTaskService.getTaskById(id);
        return R.ok(task);
    }

    /**
     * 分页查询ETL任务列表
     */
    @GetMapping
    @Operation(summary = "分页查询ETL任务列表")
    public TableDataInfo<EtlTaskVO> listTasks(PageQuery pageQuery) {
        return etlTaskService.listTasks(pageQuery);
    }

    /**
     * 执行ETL任务
     */
    @PostMapping("/{id}/execute")
    @Operation(summary = "执行ETL任务")
    @Log(title = "ETL任务管理", businessType = BusinessType.OTHER)
    public R<Long> executeTask(@Parameter(description = "任务ID") @PathVariable Long id) {
        Long instanceId = etlTaskService.executeTask(id);
        return R.ok(instanceId);
    }

    /**
     * 停止ETL任务
     */
    @PostMapping("/instances/{instanceId}/stop")
    @Operation(summary = "停止ETL任务")
    @Log(title = "ETL任务管理", businessType = BusinessType.OTHER)
    public R<Void> stopTask(@Parameter(description = "实例ID") @PathVariable Long instanceId) {
        etlTaskService.stopTask(instanceId);
        return R.ok();
    }

    /**
     * 获取执行进度
     */
    @GetMapping("/instances/{instanceId}/progress")
    @Operation(summary = "获取执行进度")
    public R<EtlProgress> getProgress(@Parameter(description = "实例ID") @PathVariable Long instanceId) {
        EtlProgress progress = etlTaskService.getProgress(instanceId);
        return R.ok(progress);
    }

    /**
     * 分页查询任务实例列表
     */
    @GetMapping("/instances")
    @Operation(summary = "分页查询任务实例列表")
    public TableDataInfo<EtlInstanceVO> listInstances(
        @Parameter(description = "任务ID") @RequestParam(required = false) Long taskId,
        PageQuery pageQuery
    ) {
        return etlTaskService.listInstances(taskId, pageQuery);
    }
}
